package com.woniu.presell.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.presell.VO.EChartsHigherVO;
import com.woniu.presell.VO.HigherVO;
import com.woniu.presell.dto.HigherDTO;
import com.woniu.presell.entity.Floor;
import com.woniu.presell.entity.Higher;
import com.woniu.presell.entity.Room;
import com.woniu.presell.entity.Storey;
import com.woniu.presell.mapper.FloorMapper;
import com.woniu.presell.mapper.HigherMapper;
import com.woniu.presell.mapper.RoomMapper;
import com.woniu.presell.mapper.StoreyMapper;
import com.woniu.presell.service.HigherService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @program: house-after
 * @description:
 * @author: qqq
 * @create: 2022-12-06 14:39
 **/
@Service
public class HigherServiceImpl implements HigherService {
    @Autowired
    private HigherMapper higherMapper;
    @Autowired
    private FloorMapper floorMapper;
    @Autowired
    private StoreyMapper storeyMapper;
    @Autowired
    private RoomMapper roomMapper;
    //查询所有楼盘信息
    @Override
    public List<HigherVO> selectAll(HigherDTO higherDTO) {
        PageHelper.startPage(higherDTO.getCurrentPage(),higherDTO.getPageSize());
        return higherMapper.selectAll(higherDTO);
    }

    @Override
    public Map<String,Object> selectByHigherAndFloor(Long id) {
        List<Floor> floors = floorMapper.selectAll(id);
        Higher higher = higherMapper.selectByPrimaryKey(id);
        Map<String,Object> map =new HashMap();
        map.put("floors",floors);
        map.put("higher",higher);
        return map;
    }
    @Transactional
    @Override
    public int add(Higher higher,String lno) {
        higher.setHigherStatus("未过审");
        higher.setHigherLicenceno(lno);
        higherMapper.insertSelective(higher);
        List flist = finsertSelective(higher.getHigherId());
        floorMapper.myinsert(flist);
        List slist = sinsertSelective(flist);
        storeyMapper.myinsert(slist);
        List rlist = rinsertSelective(slist);
        roomMapper.myinsert(rlist);
        return higher.getHigherId();
    }

    @Override
    public List<EChartsHigherVO> selectGroupByBuilding() {
        return higherMapper.selectGroupByBuilding();
    }


    public List<Floor> finsertSelective(Integer id) {
        List<Floor> list=new ArrayList<Floor>();
        for (int i=1;i<8;i++){
            Floor floor=new Floor();
            floor.setFloorNo("000"+i);
            floor.setFloorName(i+"号楼");
            floor.setFloorAllno(7+"");
            floor.setFloorRoomno(42+"");
            floor.setFloorArea(3000+"");
            floor.setHigherId(id);
            floor.setBak1("普通公寓");
            list.add(floor);
        }
        return list;
    }

    public List<Storey> sinsertSelective(List<Floor> flist) {
        List<Storey> list=new ArrayList<Storey>();
        for(int i=0;i<flist.size();i++){
            for(int j=1;j<=flist.size();j++){
                Storey storey=new Storey();
                storey.setStoreyNo(j+"");
                storey.setFloorId(flist.get(i).getFloorId());
                list.add(storey);
            }
        }

        return list;
    }
    public List<Room> rinsertSelective(List<Storey> slist) {
        List<Room> list = new ArrayList<Room>();
        for (int i = 0; i < slist.size(); i++) {
            for (int j = 1; j < 7; j++) {
                Room room = new Room();
                room.setRoomNo(new Date().getTime() + RandomStringUtils.randomNumeric(4));
                room.setRoomStatus("1");
                room.setStoreyId(slist.get(i).getStoreyId());
                room.setBak1("https://s1.ax1x.com/2022/12/12/z4PRKg.jpg");
                room.setBak2("10000");
                room.setRoomArea("120");
                room.setRoomCommonArea("12");
                room.setRoomPropArea("10");
                list.add(room);
            }
        }
        return list;
    }
}
